package com.iwop.moto.controller;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.AnnexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/8/30.
 */
@Controller
public class AnnexController extends BaseController{
    @Autowired
     AnnexService annexService;

    /**
     * 添加首页图片
     * @参数 path 图片地址
     * */
    @RequestMapping("/api/annex/add")
    @ResponseBody
    public ResultModel addAnnex(){
        try {
             SuperRecordModel SRModel = this.getSRModel();
            SRModel.put("aid", UUID.randomUUID().toString());
            SRModel.put("types","homePage");
            SRModel.put("pid","homePage");
            SRModel.put("createdate",new Date());
            annexService.insertAnnex(SRModel);
           return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 添加首页图片
     * @参数 pid 所属id
     * */
    @RequestMapping("/api/annex/get")
    @ResponseBody
    public ResultModel getAnnex(){
        try {
            SuperRecordModel SRModel = this.getSRModel();
            List pid = annexService.getAnnex(SRModel.getString("pid"));
            return getSucceed(pid,ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 添加首页图片
     * @参数 aid 主键
     * */
    @RequestMapping("/api/annex/del")
    @ResponseBody
    public ResultModel deleteAnnex(){
        try {
            SuperRecordModel SRModel = this.getSRModel();
            annexService.deleteAnnex(SRModel.getString("aid"));
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 添加首页图片
     * @参数 aid 主键
     * 参数 path 图片路径
     * */
    @RequestMapping("/api/annex/edit")
    @ResponseBody
    public ResultModel editAnnex(){
        try {
            SuperRecordModel SRModel = this.getSRModel();
            annexService.updateAnnex(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
